//
//  AIDCTools.m
//  分享demo
//
//  Created by pc-chtypd056 on 2018/8/28.
//  Copyright © 2018年 CTL. All rights reserved.
//

#import "AIDCTools.h"
#import <UIKit/UIKit.h>

@implementation AIDCTools

+ (instancetype)shared{
    static AIDCTools *tools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[AIDCTools alloc]init];
    });
    return tools;
}

- (void)showAIDCWithStatus:(AIDCStatusBlock)statusBlock{
    if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
        dispatch_async(dispatch_get_main_queue(), ^{
            CTLog(@"系统版本不支持TouchID/FaceID (必须高于iOS 8.0才能使用)");
            statusBlock(AIDCStatusVersionNotSupport,nil);
        });
        return;
    }
    
    LAContext *context = [[LAContext alloc]init];
    
    if (@available(iOS 10.0, *)) {
        context.localizedCancelTitle = @"取消";
    } else {
        // Fallback on earlier versions
        context.localizedFallbackTitle = @"测试";
    }
    
    NSError *error = nil;
    
    if (@available(iOS 9.0, *)) {
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"测试" reply:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        CTLog(@"TouchID/FaceID 验证成功");
                        statusBlock(AIDCStatusSuccess, error);
                    });
                }else if (error){
                    if (@available(iOS 11.0, *)) {
                        switch (error.code) {
                            case LAErrorAuthenticationFailed:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 验证失败");
                                    statusBlock(AIDCStatusFail, error);
                                });
                                break;
                            }
                            case LAErrorUserCancel:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 被用户手动取消");
                                    statusBlock(AIDCStatusUserCancel, error);
                                });
                            }
                                break;
                            case LAErrorUserFallback:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"用户不使用TouchID/FaceID,选择手动输入密码");
                                    statusBlock(AIDCStatusInputPassword, error);
                                });
                            }
                                break;
                            case LAErrorSystemCancel:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 被系统取消 (如遇到来电,锁屏,按了Home键等)");
                                    statusBlock(AIDCStatusSystemCancel, error);
                                });
                            }
                                break;
                            case LAErrorPasscodeNotSet:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 无法启动,因为用户没有设置密码");
                                    statusBlock(AIDCStatusPasswordNotSet, error);
                                });
                            }
                                break;
                                //case LAErrorTouchIDNotEnrolled:{
                            case LAErrorBiometryNotEnrolled:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 无法启动,因为用户没有设置TouchID/FaceID");
                                    statusBlock(AIDCStatusTouchIDNotSet, error);
                                });
                            }
                                break;
                                //case LAErrorTouchIDNotAvailable:{
                            case LAErrorBiometryNotAvailable:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 无效");
                                    statusBlock(AIDCStatusTouchIDNotAvailable, error);
                                });
                            }
                                break;
                                //case LAErrorTouchIDLockout:{
                            case LAErrorBiometryLockout:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID/FaceID 被锁定(连续多次验证TouchID/FaceID失败,系统需要用户手动输入密码)");
                                    statusBlock(AIDCStatusTouchIDLockout, error);
                                });
                            }
                                break;
                            case LAErrorAppCancel:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"当前软件被挂起并取消了授权 (如App进入了后台等)");
                                    statusBlock(AIDCStatusAppCancel, error);
                                });
                            }
                                break;
                            case LAErrorInvalidContext:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"当前软件被挂起并取消了授权 (LAContext对象无效)");
                                    statusBlock(AIDCStatusInvalidContext, error);
                                });
                            }
                                break;
                            default:
                                break;
                        }
                    }else{
                        switch (error.code) {
                            case LAErrorAuthenticationFailed:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 验证失败");
                                    statusBlock(AIDCStatusFail, error);
                                });
                                break;
                            }
                            case LAErrorUserCancel:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 被用户手动取消");
                                    statusBlock(AIDCStatusUserCancel, error);
                                });
                            }
                                break;
                            case LAErrorUserFallback:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"用户不使用TouchID,选择手动输入密码");
                                    statusBlock(AIDCStatusInputPassword, error);
                                });
                            }
                                break;
                            case LAErrorSystemCancel:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 被系统取消 (如遇到来电,锁屏,按了Home键等)");
                                    statusBlock(AIDCStatusSystemCancel, error);
                                });
                            }
                                break;
                            case LAErrorPasscodeNotSet:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 无法启动,因为用户没有设置密码");
                                    statusBlock(AIDCStatusPasswordNotSet, error);
                                });
                            }
                                break;
                            case LAErrorTouchIDNotEnrolled:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 无法启动,因为用户没有设置TouchID");
                                    statusBlock(AIDCStatusTouchIDNotSet, error);
                                });
                            }
                                break;
                                //case :{
                            case LAErrorTouchIDNotAvailable:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 无效");
                                    statusBlock(AIDCStatusTouchIDNotAvailable, error);
                                });
                            }
                                break;
                            case LAErrorTouchIDLockout:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"TouchID 被锁定(连续多次验证TouchID失败,系统需要用户手动输入密码)");
                                    statusBlock(AIDCStatusTouchIDLockout, error);
                                });
                            }
                                break;
                            case LAErrorAppCancel:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"当前软件被挂起并取消了授权 (如App进入了后台等)");
                                    statusBlock(AIDCStatusAppCancel, error);
                                });
                            }
                                break;
                            case LAErrorInvalidContext:{
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    CTLog(@"当前软件被挂起并取消了授权 (LAContext对象无效)");
                                    statusBlock(AIDCStatusInvalidContext, error);
                                });
                            }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }];
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                CTLog(@"当前设备不支持TouchID/FaceID");
                statusBlock(AIDCStatusVersionNotSupport,error);
            });
        }
    } else {
        // Fallback on earlier versions
    }
}

@end
